package service

import (
	"context"
	"fmt"
	_ "github.com/go-sql-driver/mysql"

	"graduation/database"
	"graduation/design"

)

type LoginService struct {
}

func (self *LoginService) Login(ctx context.Context, message *design.LoginRequestMessage) (*design.LoginRespMessage, error) {
	fmt.Println(message)
	user,error := database.FilterOneUserByName(message.UserName)
	if error != nil || !user.CheckPassWord(message.PassWord) {
		return &design.LoginRespMessage{
			Code: 403,
			Msg:  "用户名密码错误",
			Data: &design.LoginRespMessage_Data{
				Token: "",
			},
		}, nil
	}
	return &design.LoginRespMessage{
		Code: 200,
		Msg:  "登录成功",
		Data: &design.LoginRespMessage_Data{
			Token: user.Token(),
		},
	}, nil

}

func (self *LoginService) Register(ctx context.Context, message *design.LoginRequestMessage)(*design.BaseRespMessage, error){
	_,error := database.RegisterUser(message.UserName,message.PassWord)
	if error!= nil{
		return &design.BaseRespMessage{
			Code:                 403,
			Msg:                  error.Error(),
		},nil
	}
	return &design.BaseRespMessage{
		Code:                 200,
		Msg:                  "新建成功",
	},nil
}




